﻿using FBroSharp.Callback;
using FBroSharp.Event;
using FBroSharp.Lib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace 资源篡改实例
{

    public class FBroResourceEventCallback : FBroSharpResourceEvent
    {
        public string url_data = @"<html>
                                     <head>
                                       <meta charset=""UTF-8"">
                                       <title>Test Cef</title>
                                     </head>
                                     <body bgcolor= ""white"">
                                     <h2>你好FBrowser！！！</h2>
                                     <a style='color:#00ff00'>这是一个直接将百度网页直接替换掉的例子！</a>
                                     <div style='color:red'>这里是个div</div>
                                     <a href='https://www.baidu.com'>打开百度网页(其实你永远都打不开，哈哈)</a>
                                     </body>
                                     <script>alert('你好FBrowser')</script></html>";

        public void Cancel(long flag)
        {
            Console.WriteLine(MethodBase.GetCurrentMethod().Name);
        }

        public void End(long flag)
        {
            Console.WriteLine(MethodBase.GetCurrentMethod().Name);
        }

        public void GetResponseHeaders(long flag, IFBroSharpResponse response, ref long response_length, ref string redirectUrl)
        {
            int size = url_data != null ? url_data.Length : 0;
            if (size > 0)
            {
                //设置响应大小为数据大小，这里不设置下面的写入数据事件将无法写入
                response_length = size;
                //设置响应类型
                response.SetMimeType("text/html");
                //设置响应状态
                response.SetStatus(200);
            }
            Console.WriteLine(MethodBase.GetCurrentMethod().Name);
        }

        public bool Open(long flag, IFBroSharpRequest request, ref bool handle_request, IFBroSharpCallback callback)
        {
            Console.WriteLine(MethodBase.GetCurrentMethod().Name);
            return false;
        }

        public bool ProcessRequest(long flag, IFBroSharpRequest request, IFBroSharpCallback callback)
        {
            Console.WriteLine(MethodBase.GetCurrentMethod().Name);
            callback.Continue();
            //返回真开始处理响应，返回假不处理
            return true;
        }

        public bool Read(long flag, out byte[] data_out, int bytes_to_read, IFBroSharpResourceReadCallback callback)
        {
            Console.WriteLine(MethodBase.GetCurrentMethod().Name);

            int size = url_data != null ? url_data.Length : 0;
            if (size > 0)
            {
                //注意这里如果size大于bytes_to_read就要分包处理,例子里面用的小数据，所以这里其实不会过来

                //小于最大可写入大小
                if (size < bytes_to_read)
                {
                    //注意这里要使用utf8
                    data_out = System.Text.Encoding.UTF8.GetBytes(url_data);

                    //写完了后将数据设置为空，避免重复写入，不然程序还会调用read直到返回false才停止
                    url_data = "";
                    //返回真生效,且继续
                    return true;
                }
                //大于最大可写入大小
                else
                {         
                    //只取最大长度部分写入
                    string temp_str = url_data.Substring(0, bytes_to_read);
                    data_out = System.Text.Encoding.UTF8.GetBytes(temp_str);
                    //保存剩余部分，以便下次读写
                    url_data = url_data.Substring(bytes_to_read, size - bytes_to_read);
                    //返回真生效,且继续
                    return true;
                }
            }

            data_out = null;
            return false;


        }

        public bool Skip(long flag, long bytes_to_skip, ref long bytes_skipped, IFBroSharpResourceSkipCallback callback)
        {
            Console.WriteLine(MethodBase.GetCurrentMethod().Name);
            return false;
        }

        public void Start(long flag)
        {
            Console.WriteLine(MethodBase.GetCurrentMethod().Name);
        }
    }

}
